<template>
  <div>
    <div class="container">
      <header>
        <button @click="$router.go(-1)" class="back-button">&lt;</button>
        <h1>网点查询</h1>
        <button class="more-button">···</button>
      </header>
      <div class="map" ref="mapRef"></div>
      <div class="search-bar">
        <select class="dropdown">
          <option>宁波</option>
          <option>上海</option>
          <option>杭州</option>
        </select>
        <div class="search-input">
          <van-icon name="search" class="search-icon" @click="searchBranches" />
          <input type="text" placeholder="查找网点" v-model="searchQuery" />
        </div>
      </div>
      <div class="branch-list-title">网点列表</div>
      <ul class="branch-list">
        <li class="branch" v-for="branch in filteredBranches" :key="branch.name">
          <h3><img :src="smileIcon" class="icon" /> {{ branch.name }}</h3>
          <p class="services">
            <span class="indent">贵宾服务</span> <span>综合服务</span> <span>提金服务</span>
          </p>
          <p><img :src="locationIcon" class="icon" /> {{ branch.address }}</p>
          <p v-if="branch.workingHours" class="working-hours">
            <img :src="clockIcon" alt="Clock Icon" class="clock-icon" />
            <span class="working-hours-text">{{ branch.workingHours }}</span>
          </p>
          <div class="branch-actions">
            <van-button type="default" @click="startNavigation(branch)" class="custom-button">
              <img :src="navigationIcon" class="icon" /> 开始导航
            </van-button>
            <van-button type="default" @click="callBranch(branch)" class="dianhua-button">
              <img :src="phoneIcon" class="icon" /> 电话咨询
            </van-button>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue';
import AMapLoader from '@amap/amap-jsapi-loader';

// 导入SVG文件
import smileIcon from '../page4/assets_page3/smile.svg';
import locationIcon from '../page4/assets_page3/location.svg';
import clockIcon from '../page4/assets_page3/clock.svg';
import navigationIcon from '../page4/assets_page3/daohang.svg';
import phoneIcon from '../page4/assets_page3/dianhua.svg';

export default {
  setup() {
    const searchQuery = ref('');
    const branches = ref([
      {
        name: '总行营业部营业室',
        address: '宁波市高新区438号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5896, 29.9997]
      },
      {
        name: '嘻嘻支行',
        address: '宁波市瓜瓜街916号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5580, 29.8735]
      },
      {
        name: '葫芦娃支行',
        service: '外汇服务 综合服务',
        address: '宁波市瓜瓜街916号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5580, 29.8735]
      },
      {
        name: '哇哈哈支行',
        service: '外汇服务 综合服务',
        address: '宁波市瓜瓜街916号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5580, 29.8735]
      },
      {
        name: '海镇支行1',
        service: '外汇服务 综合服务',
        address: '宁波市海震街916号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5580, 244.8735]
      },
      {
        name: '海镇支行2',
        service: '外汇服务 综合服务',
        address: '宁波市海震街916号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5580, 244.8735]
      },
      {
        name: '海镇支行3',
        service: '外汇服务 综合服务',
        address: '宁波市海震街916号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5580, 244.8735]
      },
      {
        name: '海镇支行4',
        service: '外汇服务 综合服务',
        address: '宁波市海震街916号',
        workingHours: '对公（工作日）：8:15-11:30 14:00-17:00\n对私（工作日）：8:30-16:30',
        location: [121.5580, 244.8735]
      }
    ]);
    const mapRef = ref(null);

    const initAMap = () => {
      window.AMapSecurityConfig = {
        securityJsCode: 'a2b555f0435734445c48efad9828f2b4',
      };

      AMapLoader.load({
        key: "1d9c8499ccfa9b3df4c51f0f73b8d4d5",
        version: "2.0",
        plugins: [
          "AMap.Scale",
          "AMap.ToolBar",
          "AMap.Geocoder",
          "AMap.CitySearch",
          "AMap.Geolocation",
          "AMap.InfoWindow"
        ]
      }).then((AMap) => {
        const map = new AMap.Map(mapRef.value, {
          resizeEnable: true,
          zoom: 11,
          center: branches.value[0]?.location || [121.5580, 29.8735],
        });

        branches.value.forEach(branch => {
          const marker = new AMap.Marker({
            position: branch.location,
            title: branch.name,
          });

          marker.setMap(map);

          marker.on('click', () => {
            alert(`这是 ${branch.name}`);
          });
        });
      }).catch(e => {
        console.error('高德地图加载失败:', e);
        alert('地图加载失败，请稍后再试。');
      });
    };

    const filteredBranches = computed(() => {
      const query = searchQuery.value.toLowerCase();
      return branches.value.filter(branch => branch.name.toLowerCase().includes(query));
    });

    const startNavigation = (branch) => {
      alert(`开始导航到: ${branch.name}`);
    };

    const callBranch = (branch) => {
      alert(`拨打电话给: ${branch.name}`);
    };

    const searchBranches = () => {
      console.log('搜索:', searchQuery.value);
    };

    onMounted(() => {
      initAMap();
    });

    return {
      searchQuery,
      filteredBranches,
      startNavigation,
      callBranch,
      searchBranches,
      mapRef,
      smileIcon,
      locationIcon,
      clockIcon,
      navigationIcon,
      phoneIcon
    };
  }
};
</script>

<style scoped>
.clock-icon {
  vertical-align: middle; /* 调整垂直对齐方式 */
  margin-top: 0px; /* 调整上边距 */
  margin-bottom: 10px; /* 调整下边距 */
  width:10px;

  height:10px;
}
/* 调整按钮大小 */
.custom-button, .dianhua-button {
  height: 2rem;
  padding: 0px 2px;
  font-size: 10px; /* 调整按钮的字体大小 */
  
}

header {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

h1 {
  padding: 0 20px;
  font-size: 20px;
  color: #444;
}

.back-button {
  position: absolute;
  left: 0px;
  font-size: 24px;
  background: none;
  border: none;
  cursor: pointer;
}

.more-button {
  position: absolute;
  right: 0;
  font-size: 24px;
  background: none;
  border: none;
  cursor: pointer;
}

.container {
  position: relative;
  margin: auto;
  padding: 20px;
  z-index: 1;
  background-image: url('../page4/assets_page3/bg.svg');
  border-radius: 10px;
  width: 100%;
  box-sizing: border-box;
  height: auto;
}

.map {
  width: 100%;
  height: 349px;
  background-color: #e0e0e0;
  margin-bottom: 20px;
}

.search-bar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.dropdown {
  margin-right: 5px;
  border: none;
  background-color: transparent;
  font-size: 16px;
  cursor: pointer;
}

.search-input {
  position: relative;
  display: flex;
  align-items: center;
  border: 1px solid #ccc;
  border-radius: 20px;
  padding: 5px 10px;
  background-color: white;
  flex: 1;
  margin-left: 10px;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
}

input[type="text"] {
  padding-left: 30px;
  width: calc(100% - 30px);
  border: none;
  outline: none;
  font-size: 16px;
}

.branch-list-title {
  font-family: '微软雅黑', sans-serif;
  font-size: 18px;
  text-align: left;
  line-height: 1;
  padding-bottom: 20px;
}

.branch-list {
  list-style: none;
  padding: 0;
  width: 100%;
}

.branch {
  position: relative;
  width: 100%;
  height: 140px;
  margin-bottom: 10px;
  padding: 15px;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  background-color: white;
  box-sizing: border-box;
  font-size: 10px;
}

.branch h3 {
  margin: 0 0 10px;
  font-size: 16px;
  text-align: left;
  font-weight: normal;
}

.branch p {
  margin: 5px 0;
  color: #555;
  font-size: 10px;
  text-align: left;
}

.branch p.working-hours {
  white-space: pre-wrap;
}

.branch-actions {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-65%);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.icon {
  width: 10px; /* 调整图标的宽度 */
  height: 10px; /* 调整图标的高度 */
  vertical-align: middle; /* 使图标垂直居中 */
}

.services span {
  font-size: 12px; /* 调整服务项字体大小 */
}

.working-hours {
  font-size: 12px; /* 调整工作时间字体大小 */
}

.working-hours-text {
  display: inline-block;
  vertical-align: middle;
  margin-left: 5px; /* 调整图标和文本之间的间距 */
}
</style>
